package com.cloudclassroom.vo;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author 92110
 */
public record RestResult<T> (int code, T data, String message) {
    //写几个工具方法，用于快速创建RestBean对象
    public static <T> RestResult<T> success(T data){
        return new RestResult<>(200, data, "请求成功");
    }

    public static <T> RestResult<T> failure(int code, String message){
        return new RestResult<>(code, null, message);
    }

    public static <T> RestResult<T> failure(int code){
        return failure(code, "请求失败");
    }

    public String toJsonString() throws JsonProcessingException {
        return new ObjectMapper().writeValueAsString(this);
    }
}